package com.ulrica.morning.tools;

import com.ulrica.morning.dto.PoetryDTO;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author LongKui
 * @since 2022/04/25
 */
public class CrawlerTool {

    public static void main(String[] args) {
        String uri = "https://www.gushici.net";
        String path = "/index_100.html";
        List<PoetryDTO> poetryDTOList = new ArrayList<>();
        String href = readHtml(uri, path, poetryDTOList);
        System.out.println("href======:" + href);
    }

    @SneakyThrows
    public static String readHtml(String uri, String path, List<PoetryDTO> poetryDTOList) {
        String url = Objects.isNull(path) ? uri : uri + path;
        Connection connect = Jsoup.connect(url);
        Document doc = connect.get();

        Elements select = doc.select("div[class='gushici-box']");
        for (Element e : select) {
            // 诗名
            String tit = e.select("p[class='tit']").text();
            String[] source = e.select("p[class='source']").text().split("：");
            Elements contents = e.select("div[class='gushici-box-text']");
            StringBuilder content = new StringBuilder();
            for (Element p : contents.select("a")) {
                content.append(String.format("%s%n", p.text()));
            }
            PoetryDTO poetryDTO = PoetryDTO.builder()
                    .poetryHashCode(tit.hashCode())
                    .name(tit)
                    .dynasty(source[0])
                    .author(source[1])
                    .content(content.toString())
                    .build();
            poetryDTOList.add(poetryDTO);
        }
//        poetryDTOList.forEach(System.out::println);

//        Elements box = doc.select("div[class='top-box']");
////        System.out.println(box.text());


        // 获取URL连接
        Elements links = doc.select("a[href]");
        for (Element link : links) {
            if ("下一页".equals(link.text())) {
                String href = link.attr("href");
//                System.out.println(href);
//                System.out.println(link.text());
                return href;
            }
        }
        return null;
    }

    @SneakyThrows
    public static void baiKe() {
        String encode = URLEncoder.encode("李白", "utf-8");
        Connection connect = Jsoup.connect("https://baike.baidu.com/item/" + encode);
        Document doc = connect.get();
        Elements elements = doc.body().select("div[class=\"lemma-summary\"]");
        elements = elements.select("div[class=\"para\"]");
        for (Element element : elements) {
            String content = element.text();
            Elements sups = element.select("sup[class=\"sup--normal\"]");
            for (Element sup : sups) {
                content = content.replace(sup.text(), "");
                content = content.replace(" ", "");
            }
            System.out.println(content);
        }
    }
}
